package com.atguigu.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.ResultCode;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantPropertiesUtilsTwo;
import com.atguigu.vod.utils.InitObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Slf4j
@Service
public class VodServiceImpl implements VodService {

    @Override
    public String uploadAliyunVideo(MultipartFile file) {

        try {
            String accessKeyId = ConstantPropertiesUtilsTwo.KEY_ID;
            String accessKeySecret = ConstantPropertiesUtilsTwo.KEY_SECRET;
            //fileName：上传文件原始名称
            String fileName = file.getOriginalFilename();
            String title = fileName.substring(0, fileName.lastIndexOf("."));

            InputStream inputStream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = response.getVideoId();
            if (response.isSuccess()) {
                return videoId;
            } else {
                log.error(response.getCode(), response.getMessage());
                return videoId;
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new GuliException(ResultCode.ERROR, e.getMessage());
        }
    }

    @Override
    public void deleteAliyunVideo(String videoId) {
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantPropertiesUtilsTwo.KEY_ID, ConstantPropertiesUtilsTwo.KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            //支持传入多个视频ID，多个用逗号分隔
            request.setVideoIds(videoId);
            DeleteVideoResponse response = client.getAcsResponse(request);
            log.info("删除视频方法请求出参 RequestId = " + response.getRequestId());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "视频删除失败");
        }
    }

    @Override
    public void deleteBatchAliyunVideo(List<String> videoList) {
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantPropertiesUtilsTwo.KEY_ID, ConstantPropertiesUtilsTwo.KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            //支持传入多个视频ID，多个用逗号分隔
            String videoIds = videoList.get(0);
            for (String s : videoList) {
                videoIds = videoIds + ", " + s;
            }
            request.setVideoIds(StringUtils.join(videoList.toArray(), ","));
            DeleteVideoResponse response = client.getAcsResponse(request);
            log.info("删除视频方法请求出参 RequestId = " + response.getRequestId());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "视频删除失败");
        }
    }

    @Override
    public String getVideoPlayAuthById(String vodeoId) {
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantPropertiesUtilsTwo.KEY_ID, ConstantPropertiesUtilsTwo.KEY_SECRET);
            // 创建获取视频地址request和response
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

            // 向request对象里面设置视频id
            request.setVideoId(vodeoId);

            // 调用初始化对象里面的方法，传递request，获取数据
            response = client.getAcsResponse(request);
            return response.getPlayAuth();
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "获取凭证失败");
        }
    }
}
